const baseURL = 'https://pcapi-xiaotuxian-front-devtest.itheima.net'
const addInterceptorrequest = {
  invoke(args) {
    // 添加loading
    uni.showLoading({
      title: '加载中'
    })
    // request 触发前拼接 url
    console.log(args.url)
    if (!args.url.startsWith('http')) {
      args.url = baseURL + args.url
    }
    args.timeout = 5000
    args.header = {
      ...args.header,
      'source-client': 'miniapp'
    }
  },
  success(args) {
    // 请求成功后，修改code值为1
    args.data.code = 1
  },
  fail(err) {
    console.log('interceptor-fail', err)
  },
  complete(res) {
    uni.hideLoading()
    console.log('interceptor-complete', res)
  }
}
uni.addInterceptor('request', addInterceptorrequest)

export default function (options) {
  return uni.request(options)
}
